package com.acxy.ants.common.kit;

import java.util.Arrays;
import java.util.List;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-08-30
 */
public class ImageKit {

    private static final List<String> allowType = Arrays.asList("image/bmp","image/png","image/gif","image/jpg","image/jpeg","image/pjpeg");

    /**
     * 校验文件是否为图片
     * @param contentType 请求类型
     * @return
     */
    public static boolean check(String contentType){
        return allowType.contains(contentType);
    }

//    /**
//     * 重新设置图像大小
//     * @param sfile 图片源文件
//     * @param tfile 目标文件
//     * @param newWidth 新宽度
//     * @param newHeight 新高度
//     * @param quality 质量系数
//     */
//    public static void resizeImg(String sfile, String tfile, int newWidth,
//                                 int newHeight, float quality) {
//        if(quality > 1) {
//            throw new IllegalArgumentException(
//                    "Quality has to be between 0 and 1");
//        }
//
//        if(newWidth <=0 || newHeight<=0) {
//            throw new IllegalArgumentException(
//                    "Width or Height must greater than zero");
//        }
//
//        //This code ensures that all the pixels in the image are loaded.
//        Image srcImg = new ImageIcon(sfile).getImage();
//        Image resizedImage = srcImg.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
//
//        // This code ensures that all the pixels in the image are loaded.
//        Image temp = new ImageIcon(resizedImage).getImage();
//
//        // Create the buffered image.
//        BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
//        // Copy image to buffered image.
//        Graphics g = bufferedImage.createGraphics();
//
//        // Clear background and paint the image.
//        g.setColor(Color.white);
//        g.fillRect(0, 0, newWidth, newHeight);
//        g.drawImage(temp, 0, 0, null);
//        g.dispose();
//
//        // Soften.
//        float softenFactor = 0.05f;
//        float[] softenArray = { 0, softenFactor, 0, softenFactor,
//                1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
//        Kernel kernel = new Kernel(3, 3, softenArray);
//        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
//        bufferedImage = cOp.filter(bufferedImage, null);
//
//        // Write the jpeg to a file.
//        FileOutputStream out = null;
//
//        try {
//            out = new FileOutputStream(new File(tfile));
//
//            // Encodes image as a JPEG data stream
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//
//            JPEGEncodeParam param = encoder
//                    .getDefaultJPEGEncodeParam(bufferedImage);
//
//            param.setQuality(quality, true);
//
//            encoder.setJPEGEncodeParam(param);
//            encoder.encode(bufferedImage);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (out != null) {
//                try {
//                    out.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//    }
}
